package com.ruifu.bom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruifu.bom.dto.MaterielImportDTO;
import com.ruifu.bom.mapper.MaterielMapper;
import com.ruifu.bom.mapper.MaterielVersionsMapper;
import com.ruifu.bom.service.MaterielService;
import com.ruifu.bom.service.MaterielVersionsService;
import com.ruifu.common.po.Materiel;
import com.ruifu.common.po.MaterielVersions;
import com.ruifu.common.utils.BeanConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 物料业务逻辑层实现类
 *
 * @author nyr
 * @create 2019/12/19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MaterielServiceImpl extends ServiceImpl<MaterielMapper, Materiel> implements MaterielService {
    @Autowired(required = false)
    private MaterielMapper materielMapper;
    @Autowired(required = false)
    private MaterielVersionsService materielVersionsService;

    @Override
    public void saveImport(List<MaterielImportDTO> materielImportDTOList) {
        List<Materiel> materielList = BeanConverter.copyList(materielImportDTOList, Materiel.class);
        List<Long> longList = new ArrayList<>();
        for (Materiel materiel : materielList) {
            if (materiel.getMlClass().equals("产品")) {
                materiel.setMlClass("g");
            }
            if (materiel.getMlClass().equals("标准件")) {
                materiel.setMlClass("s");
            }
            if (materiel.getMlClass().equals("零部件")) {
                materiel.setMlClass("p");
            }
            materielMapper.insert(materiel);
            longList.add(materiel.getMlId());
        }
        if (materielImportDTOList.isEmpty()) {
            materielVersionsService.saveImport(materielImportDTOList, longList);
        }
    }
}